/* eslint-disable max-lines */
import React from 'react';
import styles from './style.css';
import PropTypes from 'prop-types';
import {FORM_OPTIONS} from '../constants';
import MultipleSelect from './MultipleSelect';
import {faultItemQuickly} from '../SubClaim/api';
import TextArea from 'Shared/components/TextArea';
import TextInput from 'Shared/components/TextInput';
import WrappedSelect from '../common/WrappedSelect';
import TagSelect from 'Shared/components/TagSelect';
import SearchInput from 'Shared/components/SearchInput';
import PersonnelSelectPanel from './PersonnelSelectPanel';
import SearchSelect from 'Shared/components/SearchSelect';
import {injectIntl, language, formatMessage} from './intl';
import {Form, Row, Col, Spin, Collapse, Button} from 'antd';
import DateRangePicker from 'Shared/components/DateRangePicker';
import DutyUnitSelectPanel from '../Claim/PageAdd/DutyUnitSelectPanel';
import PartCategorySelectPanel from '../Claim/PartCategorySelectPanel';
import FaultModeSelectPanel from '../Claim/PageAdd/FaultModeSelectPanel';
import FaultItemSelectPanel from '../SubClaim/PageAdd/FaultItemSelectPanel';
import ProductCategoryPanel from '../TechnologyActivity/PageAdd/ProductCategoryPanel';
import {getPartCategoryQuickly, faultModeQuickly, getDutyUnitsQuickly} from '../Claim/api';
import {reportFormList, isYesNo, marketSelect, vehiclePurchasePurpose, claimStatus, dutyUnitType, linkRatioTime} from '../Enum';
const isYesNoList = isYesNo.toList(language);
const yearonYearList = isYesNo.toList(language);
const claimStatusList = claimStatus.pick([
    claimStatus.驳回,
    claimStatus.拒赔,
    claimStatus.审核通过,
]);
class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
        marketSelect: 1,
        showDutyUnit: false,
        showFaultMode: false,
        showFaultItem: false,
        showPartCategory: false,
        showProductCategory: false,
        showDeterminedSupplier: false,
        showPersonnelSelectPanel: false
    }
    // eslint-disable-next-line react/sort-comp
    onResetClick = () => {
        this.setState({
            approverId: '',
            marketSelect: 1,
            showProductCategory: false,
            productCategoryId: '',
            productCategoryName: ''
        });
        this.props.onResetClick();
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    reportFormChange = (value, name) => {
        this.props.onConditionsChange(name, value);
        // eslint-disable-next-line no-negated-condition
        if(!(value === reportFormList.索赔单据报表 ||
            value === reportFormList.单据审核报表))
            this.props.onConditionsChange('yearonYear', 1);
        else
            this.props.onConditionsChange('linkRatio', 1);
    }
    onSelectProduct = data => {
        this.props.onConditionsChange('productCategoryCode', data.code);
        this.setState({
            showProductCategory: false,
            productCategoryId: data.id,
            productCategoryName: data.name
        });
    }
    onClickPartCategory = () => {
        this.setState({showPartCategory: true});
    }
    onClosePartCategory = () => {
        this.setState({showPartCategory: false});
    }
    labelMap = d => {
        if(d.id === undefined)
            return '';
        if(d)
            return `(${d.code}) ${d.name}`;
        return '';
    }
    onSearchPartCategory = value =>
        getPartCategoryQuickly({
            query: value
        })
            .then(res => {
                const data = res.data;
                if(data)
                    return data.content;
            });
    onSelectPartCategory = data => {
        this.props.onConditionsChange('causePartCategoryId', data.id);
        this.props.onQueryPartCategoryChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
        this.setState({
            showPartCategory: false
        });
    }
    onClickStopPropagation = e => {
        e.stopPropagation();
    }
    onSelectFaultItem = data => {
        this.props.onConditionsChange('faultItemId', data.id);
        this.props.onQueryFaultItemChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
        this.setState({
            showFaultItem: false
        });
    }
    onSelectFaultMode = data => {
        this.props.onConditionsChange('faultModeId', data.id);
        this.props.onQueryFaultModeChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
        this.setState({
            showFaultMode: false
        });
    }
    onSelectDutyUnit = data => {
        this.props.onConditionsChange('dutyUnitId', data.id);
        this.props.onQueryDutyUnitChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
        this.setState({
            showDutyUnit: false
        });
    }
    onSelectDeterminedSupplier = data => {
        this.props.onConditionsChange('determinedSupplierId', data.id);
        this.props.onQueryDeterminedSupplierChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
        this.setState({
            showDeterminedSupplier: false
        });
    }
    onSelectPersonnel = data => {
        this.props.onConditionsChange('approverName', data.name);
        this.setState({
            approverId: data.id,
            showPersonnelSelectPanel: false
        });
    }
    onClickFaultItem = () => {
        this.setState({showFaultItem: true});
    }
    onCloseFaultItem = () => {
        this.setState({showFaultItem: false});
    }
    onSearchFaultItem = value =>
        faultItemQuickly({
            query: value
        })
            .then(res => {
                const data = res.data;
                if(data)
                    return data.content;
            });
    onSearchFaultMode = value =>
        faultModeQuickly({filter: value})
            .then(res => {
                const data = res.data;
                if(data)
                    return data.content;
            });
    onSearchDutyUnit = value =>
        getDutyUnitsQuickly({
            filter: value,
            type: dutyUnitType.索赔供应商
        })
            .then(res => {
                const data = res.data;
                if(data)
                    return data.content;
            });
    onSearchDeterminedSupplier = value =>
        getDutyUnitsQuickly({
            filter: value,
            type: dutyUnitType.责任单位
        })
            .then(res => {
                const data = res.data;
                if(data)
                    return data.content;
            });
    onClickFaultMode = () => {
        this.setState({showFaultMode: true});
    }
    onCloseFaultMode = () => {
        this.setState({showFaultMode: false});
    }
    onClickDutyUnit = () => {
        this.setState({showDutyUnit: true});
    }
    onCloseDutyUnit = () => {
        this.setState({showDutyUnit: false});
    }
    onClickDeterminedSupplier = () => {
        this.setState({showDeterminedSupplier: true});
    }
    onCloseDeterminedSupplier = () => {
        this.setState({showDeterminedSupplier: false});
    }
    onClickSearchPersonnelBtn = () => {
        this.setState({showPersonnelSelectPanel: true});
    }
    onClosePersonnelSelectPanel = () => {
        this.setState({showPersonnelSelectPanel: false});
    }
    handleMarketSelect = value => {
        this.setState({marketSelect: value});
        this.props.onConditionsChange('region', []);
        this.props.onConditionsChange('dealerCode', []);
    }
    render() {
        const {...rest} = this.state;
        const {conditions, loading, queryFaultMode,
            queryFaultItem, queryPartCategory, queryDutyUnit,
            categories, queryDeterminedSupplier} = this.props;
        const productCategory = rest.productCategoryId
            ? `${conditions.productCategoryCode}(${rest.productCategoryName})` : null;
        const partCategoryInfo = conditions.causePartCategoryId ? {
            id: queryPartCategory.id,
            code: queryPartCategory.code,
            name: queryPartCategory.name
        } : null;
        const faultItemInfo = conditions.faultItemId ? {
            id: queryFaultItem.id,
            code: queryFaultItem.code,
            name: queryFaultItem.name
        } : null;
        const faultModeInfo = conditions.faultModeId ? {
            id: queryFaultMode.id,
            code: queryFaultMode.code,
            name: queryFaultMode.name,
        } : null;
        const dutyUnitInfo = conditions.dutyUnitId ? {
            id: queryDutyUnit.id,
            code: queryDutyUnit.code,
            name: queryDutyUnit.name,
        } : null;
        const determinedSupplierInfo = conditions.determinedSupplierId ? {
            id: queryDeterminedSupplier.id,
            code: queryDeterminedSupplier.code,
            name: queryDeterminedSupplier.name,
        } : null;
        const hb = !(conditions.reportForm === reportFormList.索赔单据报表 ||
            conditions.reportForm === reportFormList.单据审核报表);
        const dis = conditions.reportForm === reportFormList.索赔单据量 ||
            conditions.reportForm === reportFormList.一次通过率 ||
            conditions.reportForm === reportFormList.通过率;
        return (
            <div className={styles.collapse}>
                <Spin spinning={loading}>
                    <Collapse>
                        <Collapse.Panel key="1" disabled={true} header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.dimension',
                                    defaultMessage: '维度'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.reportForm',
                                                    defaultMessage: '报表'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                    name="reportForm"
                                                    value={conditions.reportForm}
                                                    options={reportFormList.toList(language)}
                                                    onChange={this.reportFormChange} /></div>
                                            </Form.Item>
                                        </Col>
                                        {(conditions.reportForm === reportFormList.索赔单据报表 ||
                                            conditions.reportForm === reportFormList.单据审核报表) &&
                                            <Col {...FORM_OPTIONS.col}>
                                                <Form.Item
                                                    label={formatMessage({
                                                        id: 'queryPanel.form.yearonYear',
                                                        defaultMessage: '同比'
                                                    })}
                                                    {...FORM_OPTIONS.item}>
                                                    <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                        name="yearonYear"
                                                        value={conditions.yearonYear}
                                                        options={yearonYearList}
                                                        onChange={this.handleFilterChange} /></div>
                                                </Form.Item>
                                            </Col>}
                                        {hb &&
                                            <Col {...FORM_OPTIONS.col}>
                                                <Form.Item
                                                    label={formatMessage({
                                                        id: 'queryPanel.form.linkRatio',
                                                        defaultMessage: '环比'
                                                    })}
                                                    {...FORM_OPTIONS.item}>
                                                    <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                        name="linkRatio"
                                                        value={conditions.linkRatio}
                                                        options={isYesNoList}
                                                        disabled={dis}
                                                        onChange={this.handleFilterChange} /></div>
                                                </Form.Item>
                                            </Col>}
                                        {hb &&
                                            <Col {...FORM_OPTIONS.col}>
                                                <Form.Item
                                                    label={formatMessage({
                                                        id: 'queryPanel.form.linkRatioTime',
                                                        defaultMessage: '环比范围'
                                                    })}
                                                    {...FORM_OPTIONS.item}>
                                                    <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                        name="linkRatioTime"
                                                        value={conditions.linkRatioTime}
                                                        options={linkRatioTime.toList(language)}
                                                        onChange={this.handleFilterChange} /></div>
                                                </Form.Item>
                                            </Col>}
                                    </Form>
                                </Row>
                            </Col></Row>} />
                        <Collapse.Panel key="2" disabled={true} header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.marketInfo',
                                    defaultMessage: '市场信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.marketSelect',
                                                    defaultMessage: '市场选择'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><WrappedSelect
                                                    name="marketSelect"
                                                    value={rest.marketSelect}
                                                    options={marketSelect.toList(language)}
                                                    onChange={this.handleMarketSelect} /></div>
                                            </Form.Item>
                                        </Col>
                                        {rest.marketSelect === 2 &&
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.marketDepartmentId',
                                                    defaultMessage: '区域'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><MultipleSelect
                                                    name="region"
                                                    placeholder={formatMessage({
                                                        id: 'queryPanel.form.region.placeholder',
                                                        defaultMessage: '最多选择1000个大区名称，以回车隔开'
                                                    })}
                                                    dataOptions={categories}
                                                    value={conditions.region}
                                                    onBlur={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>}
                                        {rest.marketSelect === 3 &&
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.dealerId',
                                                    defaultMessage: '总代'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><TextArea
                                                    name="dealerCode"
                                                    placeholder={formatMessage({
                                                        id: 'queryPanel.form.dealerCode.placeholder',
                                                        defaultMessage: '最多输入1000个总代名称，以回车隔开'
                                                    })}
                                                    value={conditions.dealerCode}
                                                    onBlur={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>}
                                    </Form>
                                </Row>
                            </Col>
                        </Row>} />
                        <Collapse.Panel key="3" header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.claimInfo',
                                    defaultMessage: '索赔信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.productCategoryId',
                                                    defaultMessage: '车型编号'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchInput
                                                    value={productCategory}
                                                    onClickSearchBtn={() => this.setState({showProductCategory: true})}/></div>
                                            </Form.Item>
                                        </Col>
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.partCategoryId',
                                                    defaultMessage: '备件分类编号'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchSelect
                                                    allowClear
                                                    value={partCategoryInfo}
                                                    onSelect={this.onSelectPartCategory}
                                                    onSearch={this.onSearchPartCategory}
                                                    keyIndex="id"
                                                    labelMap={this.labelMap}
                                                    onClickSearchBtn={this.onClickPartCategory} /></div>
                                            </Form.Item>
                                        </Col>
                                    </Form>
                                </Row>
                            </Col>
                        </Row>}>
                            <Form className="form-standard">
                                <Row>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.engineCode',
                                                defaultMessage: '发动机型号'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <TextInput
                                                name="engineCode"
                                                value={conditions.engineCode}
                                                onPressEnter={this.handleFilterPressEnter}
                                                onBlur={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.transmissionSn',
                                                defaultMessage: '变速箱号'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <TextInput
                                                name="transmissionSn"
                                                value={conditions.transmissionSn}
                                                onPressEnter={this.handleFilterPressEnter}
                                                onBlur={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.vehiclePurchasePurpose',
                                                defaultMessage: '车辆用途'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <WrappedSelect
                                                name="vehiclePurchasePurpose"
                                                value={conditions.vehiclePurchasePurpose}
                                                options={vehiclePurchasePurpose.toList(language)}
                                                onChange={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.causePartCode',
                                                defaultMessage: '主损件编号'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <TextInput
                                                name="causePartCode"
                                                value={conditions.causePartCode}
                                                onPressEnter={this.handleFilterPressEnter}
                                                onBlur={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.causePartName',
                                                defaultMessage: '主损件名称'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <TextInput
                                                name="causePartName"
                                                value={conditions.causePartName}
                                                onPressEnter={this.handleFilterPressEnter}
                                                onBlur={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>

                                </Row>
                            </Form>
                        </Collapse.Panel>
                        <Collapse.Panel key="4" disabled={true} header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.problemInfo',
                                    defaultMessage: '问题信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.faultItemId',
                                                    defaultMessage: '故障代码'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchSelect
                                                    allowClear
                                                    value={faultItemInfo}
                                                    onSelect={this.onSelectFaultItem}
                                                    onSearch={this.onSearchFaultItem}
                                                    keyIndex="id"
                                                    labelMap={this.labelMap}
                                                    onClickSearchBtn={this.onClickFaultItem} /></div>
                                            </Form.Item>
                                        </Col>
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.faultMode',
                                                    defaultMessage: '故障原因'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchSelect
                                                    value={faultModeInfo}
                                                    onSelect={this.onSelectFaultMode}
                                                    onSearch={this.onSearchFaultMode}
                                                    keyIndex="id"
                                                    labelMap={this.labelMap}
                                                    onClickSearchBtn={this.onClickFaultMode} /></div>
                                            </Form.Item>
                                        </Col>
                                    </Form>
                                </Row>
                            </Col>
                        </Row>} />
                        <Collapse.Panel key="5" header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.approveInfo',
                                    defaultMessage: '审核信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.determinedSupplierId',
                                                    defaultMessage: '判定供应商编号'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchSelect
                                                    value={determinedSupplierInfo}
                                                    onSelect={this.onSelectDeterminedSupplier}
                                                    onSearch={this.onSearchDeterminedSupplier}
                                                    keyIndex="id"
                                                    labelMap={this.labelMap}
                                                    onClickSearchBtn={this.onClickDeterminedSupplier} /></div>
                                            </Form.Item>
                                        </Col>
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.dutyUnitId',
                                                    defaultMessage: '责任单位编号'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}><SearchSelect
                                                    value={dutyUnitInfo}
                                                    onSelect={this.onSelectDutyUnit}
                                                    onSearch={this.onSearchDutyUnit}
                                                    keyIndex="id"
                                                    labelMap={this.labelMap}
                                                    onClickSearchBtn={this.onClickDutyUnit} /></div>
                                            </Form.Item>
                                        </Col>
                                    </Form>
                                </Row>
                            </Col>
                        </Row>}>
                            <Form className="form-standard">
                                <Row>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.approverId',
                                                defaultMessage: '审核人'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <SearchInput
                                                disabled={true}
                                                value={conditions.approverName}
                                                onClickSearchBtn={() => this.onClickSearchPersonnelBtn()} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.status',
                                                defaultMessage: '审核状态'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <TagSelect
                                                name="status"
                                                value={conditions.status}
                                                options={claimStatusList.toList(language)}
                                                onChange={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                </Row>
                            </Form>
                        </Collapse.Panel>
                        {conditions.yearonYear === 0 && <Collapse.Panel key="6" header={<Row>
                            <Col span={2} className={styles.titleName}>
                                {formatMessage({
                                    id: 'queryPanel.form.dataTimeInfo',
                                    defaultMessage: '时间信息'
                                })}
                            </Col>
                            <Col span={22}>
                                <Row>
                                    <Form className="form-standard">
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.submitTime',
                                                    defaultMessage: '提交时间'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}>
                                                    <DateRangePicker name="submitTime" value={conditions.submitTime}
                                                        onChange={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>
                                        <Col {...FORM_OPTIONS.col}>
                                            <Form.Item
                                                label={formatMessage({
                                                    id: 'queryPanel.form.approveTime',
                                                    defaultMessage: '审核时间'
                                                })}
                                                {...FORM_OPTIONS.item}>
                                                <div onClick={this.onClickStopPropagation}>
                                                    <DateRangePicker name="approveTime" value={conditions.approveTime}
                                                        onChange={this.handleFilterChange} /></div>
                                            </Form.Item>
                                        </Col>
                                    </Form>
                                </Row>
                            </Col>
                        </Row>}>
                            <Form className="form-standard">
                                <Row>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.banlaceDate',
                                                defaultMessage: '结算日期'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <DateRangePicker name="banlaceDate" value={conditions.banlaceDate}
                                                onChange={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.productionDate',
                                                defaultMessage: '生产日期'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <DateRangePicker name="productionDate" value={conditions.productionDate}
                                                onChange={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                    <Col {...FORM_OPTIONS.col}>
                                        <Form.Item
                                            label={formatMessage({
                                                id: 'queryPanel.form.saleDate',
                                                defaultMessage: '实销日期'
                                            })}
                                            {...FORM_OPTIONS.item}>
                                            <DateRangePicker name="saleDate" value={conditions.saleDate}
                                                onChange={this.handleFilterChange} />
                                        </Form.Item>
                                    </Col>
                                </Row>
                            </Form>
                        </Collapse.Panel>}
                    </Collapse>
                    <Form className={styles.fromtop}>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}
                                </Button>
                                <Button key="clear" onClick={this.onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}
                                </Button>
                            </Col>
                        </Row>
                    </Form>
                    {
                        rest.showProductCategory &&
                        <ProductCategoryPanel
                            type="claimReportForm"
                            onSelect={this.onSelectProduct}
                            currentSelectRowkeys={rest.productCategoryId}
                            onCancel={() => this.setState({showProductCategory: false})} />
                    }
                    {
                        rest.showPartCategory &&
                        <PartCategorySelectPanel
                            selectKey={queryPartCategory.id}
                            onCancel={this.onClosePartCategory}
                            onSelect={this.onSelectPartCategory} />
                    }
                    {
                        rest.showFaultItem &&
                        <FaultItemSelectPanel
                            selectKey={queryFaultItem.id}
                            onCancel={this.onCloseFaultItem}
                            onSelect={this.onSelectFaultItem} />
                    }
                    {
                        rest.showFaultMode &&
                        <FaultModeSelectPanel
                            selectKey={queryFaultMode.id}
                            onCancel={this.onCloseFaultMode}
                            onSelect={this.onSelectFaultMode} />
                    }
                    {
                        rest.showDutyUnit &&
                        <DutyUnitSelectPanel
                            type={dutyUnitType.责任单位}
                            isApprevor={false}
                            selectKey={queryDutyUnit.id}
                            onCancel={this.onCloseDutyUnit}
                            onSelect={this.onSelectDutyUnit} />
                    }
                    {
                        rest.showDeterminedSupplier &&
                        <DutyUnitSelectPanel
                            type={dutyUnitType.索赔供应商}
                            isApprevor={false}
                            selectKey={queryDeterminedSupplier.id}
                            onCancel={this.onCloseDeterminedSupplier}
                            onSelect={this.onSelectDeterminedSupplier} />
                    }
                    {
                        rest.showPersonnelSelectPanel &&
                        <PersonnelSelectPanel
                            selectKey={rest.approverId}
                            onCancel={this.onClosePersonnelSelectPanel}
                            onSelect={this.onSelectPersonnel} />
                    }
                </Spin>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    categories: PropTypes.array
};

import {connect} from 'react-redux';
import {
    saveQueryDeterminedSupplier,
    saveQueryPartCategory,
    saveQueryDutyUnit,
    saveQueryFaultMode,
    saveQueryFaultItem,
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    getListFail
} from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getCategoryData = createSelector(
    state => state.getIn(['page', 'domainData', 'category']),
    init => {
        const initData = init.toJS();
        const categories = initData.data ? initData.data.map(item => ({
            code: item.code,
            name: item.name
        })) : [];
        return categories;
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDutyUnit = selectorFactory(['page', 'appState', 'queryDutyUnit']);
const getQueryFaultItem = selectorFactory(['page', 'appState', 'queryFaultItem']);
const getQueryFaultMode = selectorFactory(['page', 'appState', 'queryFaultMode']);
const getQueryPartCategory = selectorFactory(['page', 'appState', 'queryPartCategory']);
const getQueryDeterminedSupplier = selectorFactory(['page', 'appState', 'queryDeterminedSupplier']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    categories: getCategoryData(state),
    queryDutyUnit: getQueryDutyUnit(state),
    queryFaultMode: getQueryFaultMode(state),
    queryFaultItem: getQueryFaultItem(state),
    queryPartCategory: getQueryPartCategory(state),
    queryDeterminedSupplier: getQueryDeterminedSupplier(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
});

const mapDispatchToProps = dispatch => ({
    onQueryDutyUnitChange: data => dispatch(saveQueryDutyUnit(data)),
    onQueryFaultModeChange: data => dispatch(saveQueryFaultMode(data)),
    onQueryFaultItemChange: data => dispatch(saveQueryFaultItem(data)),
    onQueryPartCategoryChange: data => dispatch(saveQueryPartCategory(data)),
    onConditionsChange: (name, value) => {
        dispatch(getListFail());
        dispatch(saveQueryCondition(name, value));
    },
    onQueryDeterminedSupplierChange: data => dispatch(saveQueryDeterminedSupplier(data)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => {
        dispatch(getListFail());
        dispatch(resetQueryPanel());
    }
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
